package com.algorhythms.infinity;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * Created by algorhythms on 6/12/13.
 */
public class CategoryDbAdapter {

    public static final String KEY_CATNAME = "catname";
    public static final String KEY_ROWID = "_id";
    public static final String KEY_TRIPID = "tripid";

    private static CategoryDbAdapter instance;
    private static final String TAG = "CategoryDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    /**
     * Make a new database!
     */
    public static final String DATABASE_CREATE_CATEGORIES = "CREATE TABLE categories" +
            "(_id INTEGER PRIMARY KEY AUTOINCREMENT, catname TEXT NOT NULL, " +
            "tripid INTEGER NOT NULL);";

    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "categories";
    private static final int DATABASE_VERSION = 2;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {
        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(TripsDbAdapter.DATABASE_CREATE_TRIPS);
            db.execSQL(ItemDbAdapter.DATABASE_CREATE_ITEMS);
            db.execSQL(CategoryDbAdapter.DATABASE_CREATE_CATEGORIES);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS categories");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * @param ctx the Context within which to work
     */
    private CategoryDbAdapter(Context ctx) {
        super();
        this.mCtx = ctx;
    }

    /**
     * Open the category table. If it cannot be opened, try to create a new
     * instance of the table. If it cannot be created, throw an exception to
     * signal the failure.
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws android.database.SQLException if the database could be neither opened or created
     */
    public CategoryDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    /**
     * @param ctx The context for the database adapter.
     * @return The singleton instance of this class.
     */
    public static CategoryDbAdapter getInstance(Context ctx) {
        if (instance == null)
            instance = new CategoryDbAdapter(ctx);

        return instance;
    }

    /**
     * Close the table.
     */
    public void close() {
        mDbHelper.close();
    }


    /**
     * Creates a new category.
     * @param catname The name of the category.
     * @param tripid The id of the associated trip.
     * @return The row ID of the category if successful; -1 otherwise.
     */
    public long createCategory(String catname, long tripid) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_CATNAME, catname);
        initialValues.put(KEY_TRIPID, tripid);
        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
     * Delete the category with the given id.
     * @param rowId The id of the category to delete.
     * @return True if the deletion was successful; false otherwise.
     */
    public boolean deleteCategory(long rowId) {
        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * @return A cursor over all categories within a particular trip.
     */
    public Cursor fetchAllCategories(long tripId) {
        // TODO diff between this and fetchCategory()
        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_CATNAME, KEY_TRIPID}
                , KEY_TRIPID + "=" + tripId, null, null, null, null);
    }

    /**
     * @param rowId The id of the category to retrieve.
     * @return A cursor positioned to the matching category (if it exists).
     * @throws android.database.SQLException if the category could not be found/retrieved
     */
    public Cursor fetchCategory(long rowId) throws SQLException {
        Cursor mCursor =
                mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID, KEY_CATNAME, KEY_TRIPID},
                        KEY_ROWID + "=" + rowId, null, null, null, null, null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * Updates the name of the category. (However, you can't change the trip to which
     * this category belongs to.)
     * @param rowId The id of category to update.
     * @param catname The new name of the category.
     * @return True if the category was successfully updated; false otherwise.
     */
    public boolean updateCategory(long rowId, String catname) {
        ContentValues args = new ContentValues();
        args.put(KEY_CATNAME, catname);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}